/**
@author		Jong Min, Lee
@date		  01/23/2014
@breif		Tool box data class 

copyright(c) Tae Young Soft. All right reserved. 2014
*/
#pragma once

#include <list>

#include "Taurus_Util/Macros.h"
#include "Taurus_ToolData/TTDefine.h"
#include "Taurus_ToolData/ToolData.h"

/**
@brief  tool box data class
*/
class TAURUS_TOOLDATA_EXPORT CToolboxData
{
private:
  DISALLOW_COPY_AND_ASSIGN(CToolboxData);

public:
  /**
  @brief		default constructor
  */
  CToolboxData();

  /**
  @brief		destructor
  */
  ~CToolboxData();

  /**
  @brief		get name of toolbox
  @return		pointer to name string
  */
  inline const TCHAR* getName() const
  {
    return m_strName;
  }

  /**
  @brief		set name of toolbox
  @param		pszName pointer to name string
  @return		ERROR_SUCCESS upon success, an error code otherwise
  */
  DWORD setName(const TCHAR* pszName);

  /**
  @brief		add tool data
  @param		pToolData pointer to tool data to be added
  @return		ERROR_SUCCESS upon success, an error code otherwise
  */
  DWORD addToolData(const CToolData* pTool);

  /**
  @brief		insert tooldata
  @param		nIndex index number in toolbox data whether new pointer to tool data is inserted
  @param    pToolData pointer to tool data to be inserted
  @return		ERROR_SUCCESS upon success, an error code otherwise
  */
  DWORD insertToolData(const unsigned nIndex, const CToolData* pToolData);

  /**
  @brief		remove tool data
  @param		nIndex index number of tool data to be removed
  @return		ERROR_SUCCESS upon success, an error code otherwise
  */
  DWORD removeToolData(const unsigned nIndex);

  /**
  @brief		remove tool data
  @param		pToolData pointer to tool data to be removed
  @return		ERROR_SUCCESS upon success, an error code otherwise
  */
  DWORD removeToolData(const CToolData* pToolData);

  /**
  @brief		remove all tool data
  */
  void removeAllToolData();

  /**
  @brief		applies function fn to each of tool data
  @param		fn unary function that accepts pointer to const monitor as argumet
            if fn return false this method stop to apply
  */
  template<class Function>
  void forEachToolData(Function fn) const
  {
    auto first = m_listTools.cbegin();
    auto last = m_listTools.cend();
    while(first != last)
    {
      if(!fn(*first)) break;
      ++first;
    }
  }

  /**
  @brief		return tool data  for which pred return true
            if no such tool data is found, this method return NULL
  @param		pred unary function that accpet pointer to tool data
  @return		pointer to tool
  */
  template<class UnaryPredicate>
  const CToolData* findToolDataIf(UnaryPredicate pred) const
  {
    auto first = m_listTools.cbegin();
    auto last = m_listTools.cend();
    while(first != last)
    {
      if(pred(*first))
        break;
      ++first;
    }

    if(first != last)
      return (*first);
    
    return NULL;
  }

  /**
  @brief		get number of pointers to tool data in this toolbox
  @return		the number of pointers to tool data
  */
  inline unsigned getNumToolData() const
  {
    return m_listTools.size();
  }


public:
  //toolbox name
  CString m_strName;

  //pointers to tool data
  std::list<const CToolData*> m_listTools;
};